package com.zqxq.lottery.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.utils.DispatcherUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.service.sys.ILotteryKindService;
import com.zqxq.lottery.entity.bo.sys.LotteryKindBO;
import com.zqxq.lottery.entity.o2o.sys.LotteryKindO2O;
import com.zqxq.lottery.entity.po.sys.LotteryKindPO;
import com.zqxq.lottery.entity.vo.sys.LotteryKindVO;
import com.zqxq.lottery.mapper.sys.LotteryKindMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 说    明：   竞猜种类
 * 创 建 人：	一刀·胡
 * 创建时间：	2018/3/16 9:38
 * 修 改 人：
 * 修改日期：
 **/
@Service
@Slf4j
public class LotteryKindServiceImpl extends ServiceImpl<LotteryKindMapper,LotteryKindPO> implements ILotteryKindService {

    @Autowired
    private YmlConfig ymlConfig;

    @Override
    public Page<LotteryKindVO> queryPage(LotteryKindBO search) throws Exception{
        Wrapper<LotteryKindPO> wrapper = new EntityWrapper<>();
        if(StrUtils.isNotEmpty(search.getSearchKey()))
            wrapper.eq("name",search.getSearchKey());
        wrapper.orderBy("sort");
        Page page=new Page(search.getPageNo(),search.getPageSize());
        List list = baseMapper.selectPage(page,wrapper);
        page.setRecords(LotteryKindO2O.toVOList(list));
        return page;
    }

    @Override
    public List<LotteryKindVO> queryList(LotteryKindBO search) throws Exception{
        return baseMapper.queryList(search);
    }

    @Override
    public LotteryKindVO save(LotteryKindVO vo) throws Exception {
        LotteryKindPO po = LotteryKindO2O.toPO(vo);
        if(StrUtils.isEmpty(vo.getId())) {
            Long id = ymlConfig.getPkId();
            vo.setId(id+"");
            po.setId(id);
            baseMapper.insert(po);
        }else{
            baseMapper.updateById(po);
        }
        return vo;
    }

    @Override
    public void updateStatus(Long id,int status) throws Exception {
        LotteryKindPO po = new LotteryKindPO();
        po.setId(id);
        po.setStatus(status);
        baseMapper.updateById(po);
    }

    @Override
    public int updateSort(Long id, int sort) throws Exception {
        LotteryKindPO po = new LotteryKindPO();
        po.setId(id);
        po.setSort(sort);
        baseMapper.updateById(po);
        baseMapper.reSort();
        po = baseMapper.selectById(DispatcherUtils.parseLong(id));
        return po.getSort();
    }
}
